function main(defaultParams) {

    debugger;

    const self = this;
    let formPartEle = this.$$element("form_part_3jhm74");

    if (!formPartEle) return;

    formPartEle.$$getRenderElement().onSave().then((res) => {
        const unitId = res.result.documentNo;

        // 获取文件 ID 和目标文档号列表
        const fileId1 = self.$$("upload_9aurrp").cValue[0];
        const fileId2 = self.$$("upload_47i7m7").cValue[0];
        const toDocumentNoList = validateAndSplit(self.$$("text_input_b7ss15").cValue);

        if (toDocumentNoList.length === 0) {
            console.error("目标文档号列表为空或格式不正确");
            return;
        }

        const toPageId = self.$$("text_input_hf0llm").cValue;

        // 构建保存参数
        const saveFileParams = createSaveFileParams(unitId, toDocumentNoList, toPageId, fileId1, fileId2);

        // 使用链式调用处理异步逻辑
        return saveUnitMedia(saveFileParams.upload_9aurrp)
            .then(() => saveUnitMedia(saveFileParams.upload_47i7m7))
            .then(() => {
                const submitPromises = toDocumentNoList.map(documentNo => submitProcess(self, defaultParams, documentNo).then(res => {
                    updateStatus(self, defaultParams, documentNo).then(r => {
                        console.log("状态修改完成...")
                    })
                    // 流程发起成功;
                    self.$message.success("批量发起成功！")
                    // 批量修改状态
                    console.log(res);
                }));
                return Promise.all(submitPromises).then(() => {
                    // 批量执行完后关闭弹窗
                    close();
                });
            });
    }).catch(err => {
        console.error("处理过程中发生错误:", err);
        // self.$message.error(err.message || "未知错误");
    });
}

function updateStatus(self, defaultParams, documentNo) {
    return new Promise((resolve, reject) => {
        api.EXEC_FORMULA("UPDATE('t_ecdf9c71ff624a45a65adb0ece7a64a8', QUERY_EQ('id','" + documentNo + "'), '', ['qualification_code', 1,'qualification_name', '待审核','documentNo', '" + documentNo + "'])").then((res) => {
            if (res && res.result) {
                console.log(res);
            }
        }).catch(err => {
            console.log(err);
        });
    })
}

/**
 * 校验并分割目标文档号列表
 * @param input - 用户输入的文档号列表
 * @returns {string[]} - 分割后的文档号数组
 */
function validateAndSplit(input) {
    if (!input || typeof input !== "string") return [];
    return input.split(",").map(item => item.trim()).filter(item => item);
}

/**
 * 创建保存文件的参数对象
 * @param unitId - 单元 ID
 * @param toDocumentNoList - 目标文档号列表
 * @param toPageId - 页面 ID
 * @returns {{upload_9aurrp: {}, upload_47i7m7: {}}} - 参数对象
 */
function createSaveFileParams(unitId, toDocumentNoList, toPageId, fileId1, fileId2) {
    return {
        upload_9aurrp: {
            unitId, fileId: fileId1, toDocumentNoList, toUniqueId: "upload_9aurrp", toPageId
        }, upload_47i7m7: {
            unitId, fileId: fileId2, toDocumentNoList, toUniqueId: "upload_47i7m7", toPageId
        }
    };
}

/**
 * 发起流程申请
 * @param self
 * @param defaultParams
 * @param documentNo
 * @returns {Promise<unknown>}
 */
function submitProcess(self, defaultParams, documentNo) {
    return new Promise((resolve, reject) => {
        const queryParams = [{
            name: "pageId", valueContent: 'a7a88151-bd54-42c5-9520-249ebbbe5c45' // 使用常量替代硬编码
        }];
        const jsonParams = {
            documentNo
        };

        api.DATA_SOURCE_EXECUTE("a2b29830-9ef5-437a-8975-476783d4e73f", "api", queryParams, [], JSON.stringify(jsonParams))
            .then(res => {
                if (!res.hasError) {
                    resolve(res);
                } else {
                    reject(new Error(res.message));
                }
            })
            .catch(err => {
                reject(new Error(err.message || "提交流程失败"));
            });
    });
}

/**
 * 保存单元媒体
 * @param params - 保存参数
 * @returns {Promise<*>}
 */
function saveUnitMedia(params) {
    return new Promise((resolve, reject) => {
        api.DATA_SOURCE_EXECUTE("ds_api:62e8fd9a-7797-4626-ba17-9635683e7a74", "api", [], [], JSON.stringify(params))
            .then(res => {
                resolve(res);
            })
            .catch(err => {
                reject(new Error(err.message || "保存媒体失败"));
            });
    });
}


/**
 * 关闭
 */
function close() {
    console.log(this);
    if (window.parent) {
        window.parent.postMessage(JSON.stringify({
            event: "close-dialog",
            param: { "XXX": "test" }
        }), "*");
    }
}
